package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.*;
import com.woniuxy.entitys.*;
import com.woniuxy.daos.TbBedDao;
import com.woniuxy.daos.TbSectionDao;
import com.woniuxy.daos.TbUserDao;
import com.woniuxy.entitys.*;
import com.woniuxy.services.SectionService;
import com.woniuxy.utils.JWTUtils;
import org.apache.ibatis.annotations.Options;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SectionServiceImpl implements SectionService {
    @Autowired
    private TbSectionDao tbSectionDao;
    @Resource
    private TbHospitalManagerDao tbHospitalManagerDao;
    @Autowired
    private TbUserDao tbUserDao;
    @Resource
    private TbUserRoleDao tbUserRoleDao;
    @Resource
    private TbDepartmentDao tbDepartmentDao;
    @Autowired
    private TbBedDao tbBedDao;
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int updateMyself(TbSection tbSection) {
        int i = tbSectionDao.updateByPrimaryKey(tbSection);
        return i;
    }
    @Override
    public TbSection getMyself(String token) {
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:"+username);
        Integer uid = loginUser.getUser().getUid();
        TbSection sectionInfoByUid = tbSectionDao.getSectionInfoByUid(uid);
        return sectionInfoByUid;
    }
    @Override
    public PageInfo<TbSection> getSection(int pageNum, int pageSize, String phone, String sname, Integer did, Integer hid) {
        PageHelper.startPage(pageNum,pageSize);
        if (hid == 0){
            return null;
        }
        List<TbSection> tbSections = tbSectionDao.selectsection(phone,sname,did,hid);
        PageInfo<TbSection> pageInfo = new PageInfo<>(tbSections);
        return pageInfo;
    }

    @Override
    public int updatePassword(Integer id) {
        TbSection tbSection = tbSectionDao.selectByPrimaryKey(id);
        TbUser tbUser = tbUserDao.selectByPrimaryKey(tbSection.getUid());
        tbUser.setUpwd("123456");
        int i = tbUserDao.updateByPrimaryKey(tbUser);
        return i;
    }

    @Override
    public int softdel(Integer id) {
        TbSection tbSection = tbSectionDao.selectByPrimaryKey(id);
        tbSection.setSoftdel(0);
        int i = tbSectionDao.updateByPrimaryKey(tbSection);
        return i;
    }

    @Override
    public Integer getHidByUid(Integer uid) {
        TbHospitalManagerExample tbHospitalManagerExample = new TbHospitalManagerExample();
        TbHospitalManagerExample.Criteria criteria = tbHospitalManagerExample.createCriteria();
        criteria.andUidEqualTo(uid);
        return tbHospitalManagerDao.selectByExample(tbHospitalManagerExample).get(0).getHid();
    }

    @Override
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    public int saveAdd(String gender, String phone, String sname,Integer hid) {
        TbUser user = new TbUser();
        user.setPhone(phone);
        user.setUpwd("123456");
        int i = tbUserDao.insertSelective(user);
        TbSection section = new TbSection();
        section.setUid(user.getUid());
        section.setGender(gender);
        section.setSname(sname);
        section.setSoftdel(1);
        section.setHid(hid);
        int i1 = tbSectionDao.insertSelective(section);
        TbUserRole tbUserRole = new TbUserRole();
        tbUserRole.setUid(user.getUid());
        tbUserRole.setRid(8);
        int i2 = tbUserRoleDao.insertSelective(tbUserRole);


        return i + i2 + i1;
    }

    @Override
    public int removeHead(Integer id) {
        TbSection tbSection = tbSectionDao.selectByPrimaryKey(id);
        tbSection.setDid(null);

        return tbSectionDao.updateByPrimaryKey(tbSection);
    }

    @Override
    public int saveAss(Integer sid, Integer did) {
        TbSection tbSection = new TbSection();
        tbSection.setSid(sid);
        tbSection.setDid(did);
        return tbSectionDao.updateByPrimaryKeySelective(tbSection);
    }

    @Override
    public List<TbDepartment> getNullDep(Integer hid) {
        List<TbDepartment> list = tbDepartmentDao.getNullDep(hid);
        return list;
    }


    @Override
    public TbSection getSectionInfoByUid(Integer uid) {
        return tbSectionDao.getSectionInfoByUid(uid);
    }

    @Override
    public List<TbBed> getAllBed(Integer pageNum, Integer pageSize, Integer status, Integer uid) {
        TbSection tbSection = getSectionInfoByUid(uid);
        PageHelper.startPage(pageNum, pageSize);
        return tbBedDao.getAllBedByDid(tbSection.getDid(), status);
    }

    @Override
    public List<TbBed> getAllBedByFuzz(Integer pageNum, Integer pageSize, String search, Integer uid) {
        TbSection tbSection = tbSectionDao.getSectionInfoByUid(uid);
        PageHelper.startPage(pageNum, pageSize);
        return tbBedDao.getAllBedByFuzz(tbSection.getDid(), search);
    }

    @Override
    public boolean addBed(TbBed tbBed) {
        tbBed.setAddTime(new Date());
        tbBed.setStatus(0);
        return tbBedDao.insert(tbBed) > 0;
    }

    @Override
    public boolean updateBed(TbBed tbBed) {
        return tbBedDao.updateByPrimaryKeySelective(tbBed) > 0;
    }



    @Override
    public boolean delSomeBed(List list) {
        TbBedExample tbBedExample = new TbBedExample();
        TbBedExample.Criteria criteria = tbBedExample.createCriteria();
        criteria.andBidIn(list);
        return tbBedDao.deleteByExample(tbBedExample) > 0;
    }
}

